﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace 订单窗体
{
    public partial class Form2 : Form
    {
        private Order _order;
        private OrderService _orderService;
        private BindingSource detailBindingSource = new BindingSource();
        private List<Product> _availableProducts = new List<Product>();
        private List<Customer> _Customers = new List<Customer>();

        public Form2(Order order, OrderService orderService,List<Customer>Customers)
        {
            InitializeComponent();
            _Customers = Customers;
            _orderService = orderService;

            if (order == null)
            {
                // 新建订单
                _order = new Order(new Customer("", ""), "");
                Text = "新建订单";
            }
            else
            {
                // 编辑现有订单
                _order = order;
                Text = "编辑订单";
            }
            //LoadAvailableProducts();
            SetupDataBindings();
            LoadAvailableProducts();
            cmbCustomerInit();
        }
        private void cmbCustomerInit()
        {
            cmbCustomer.DataSource = _Customers;
            cmbCustomer.DisplayMember = "Name";
        }
        private void SetupDataBindings()
        {
            // 订单基本信息绑定
            txtOrderId.DataBindings.Add("Text", _order, "OrderId");
            cmbCustomer.DataBindings.Add("SelectedItem", _order, "Customer");

            // 订单明细绑定
            detailBindingSource.DataSource = _order.DetailList;
            detailDataGridView.AutoGenerateColumns = false;
            detailDataGridView.DataSource = detailBindingSource;

            // 设置明细列
            detailDataGridView.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "Product",
                HeaderText = "商品",
                Name = "colProduct",
                Width = 200,
                ValueType = typeof(Product)
            });

            detailDataGridView.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "Quantity",
                HeaderText = "数量",
                Name = "colQuantity",
                Width = 80
            });

            detailDataGridView.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "Amount",
                HeaderText = "金额",
                Name = "colAmount",
                Width = 100,
                DefaultCellStyle = new DataGridViewCellStyle() { Format = "C2" }
            });
        }
        private void LoadAvailableProducts()
        {
            _availableProducts.Add(new Product("P001", "笔记本电脑", 5999.99));
            _availableProducts.Add(new Product("P002", "手机", 3999.99));
            _availableProducts.Add(new Product("P003", "耳机", 299.99));
            _availableProducts.Add(new Product("P004", "鼠标", 99.99));
            _availableProducts.Add(new Product("P005", "键盘", 199.99));

            cmbProduct.DataSource = _availableProducts;
            cmbProduct.DisplayMember = "Name";
        }

        private void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (cmbProduct.SelectedItem == null)
            {
                MessageBox.Show("请选择一个商品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (!int.TryParse(txtQuantity.Text, out int quantity) || quantity <= 0)
            {
                MessageBox.Show("请输入有效的数量", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var product = (Product)cmbProduct.SelectedItem;
            var detail = new Orderdetail(product, quantity);

            try
            {
                _order.AddDetail(detail);
                detailBindingSource.ResetBindings(false);
                // UpdateTotalAmount();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"添加失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void UpdateTotalAmount()
        {
            //lblTotalAmount.Text = _order.TotalAmount.ToString("C2");
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(_order.OrderId))
            {
                MessageBox.Show("请输入订单号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (_order.DetailList.Count == 0)
            {
                MessageBox.Show("请至少添加一个订单明细", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            try
            {
                if (_orderService.GetAllOrders().Any(o => o.OrderId == _order.OrderId && o != _order))
                {
                    MessageBox.Show("订单号已存在", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (!_orderService.GetAllOrders().Contains(_order))
                {
                    _orderService.Add(_order);
                }

                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

